/**
 * @file userList.c
 * @brief 
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2024-02-27
 * 
 * @copyright Copyright (c) 2024  by xuesong
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-02-27     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "userList.h"

static void copyItemToNode(Item item, Node * pnode);

void initList(List * plist){
    *plist = NULL;
}

bool listIsEmpty(List * plist){
    if(*plist == NULL){
        return true;
    }
    return false;
}

bool listIsFull(List * plist){
    Node * pt;
    pt = (Node *) malloc(sizeof(Node));
    if(pt == NULL){
        free(pt);
        return true;
    }else{
        free(pt);
        return false;
    }
}

/**
 * @brief  add item 2 list
 * @param  plist            MyParamDoc
 * @param  item             MyParamDoc
 * @return true 
 * @return false 
 */
bool addItem(List * plist, Item item){
    Node * pnode;
    Node * current = *plist;
    pnode = (Node *)malloc(sizeof(Node));
    if(pnode == NULL){
        return false;
    }
    copyItemToNode(item, pnode);

    pnode->next = NULL;
    if(*plist == NULL){ //如果列表中没有项目，则添加的是第一个项目
        *plist = pnode;
    }else{
        //如果不是第一个项目，则找到列表中节点为空的项目，将新项目的地址存进去
        while(current->next != NULL){
            current = current->next;
        }
        current->next = pnode;
    }
    return true;
}

bool InsertItem(List * plist, Item item)
{
    Node * pnode;
    Node * lastpnode;
    Node * current = *plist;
    pnode = (Node *)malloc(sizeof(Node));
    if(pnode == NULL){
        return false;
    }
    copyItemToNode(item, pnode);

    pnode->next = NULL;
    if(*plist == NULL){ //如果列表中没有项目，则添加的是第一个项目
        *plist = pnode;
    }else{
        //如果不是第一个项目，则找到列表中节点为空的项目，将新项目的地址存进去
        lastpnode = current;
        while(current != NULL){
            if(current->item.alarmMin < item.alarmMin)
            {
                lastpnode = current;
                current = current->next;
            }else if(current->item.alarmMin == item.alarmMin)
            {
                current->item.flag = item.flag;
                return true;
            }
            else{
                break;
            }
        }
        if(current == *plist)
        {
            *plist = pnode;
            pnode->next = current;
        }else{
            // current->next = pnode;
            current = lastpnode->next;      //back up next point
            lastpnode->next = pnode;
            pnode->next = current;
        }
    }
    return true;
}

void ListDelItem(List * plist, Item item){
    Node * lastpnode;
    Node * current = *plist;
    int count = 0;
    while(current != NULL){
        if(current->item.alarmMin == item.alarmMin)
        {
            if(current == *plist)
            {
                *plist = current->next;
            }else{
                lastpnode->next = current->next;
            }
            lastpnode = current->next;
            free(current);
            current = lastpnode;
        }
        if(current != NULL)
        {
            lastpnode = current;
            current = current->next;
            count++;
        }
    }
}

void ListDelAll(List * plist)
{
    Node * lastpnode;
    Node * current = *plist;
    int count = 0;
    while(current != NULL){
        if(current == *plist)
        {
            *plist = current->next;
        }
        lastpnode = current->next;
        free(current);
        current = lastpnode;
    }
}


/**
 * @brief  
 * @param  plist            MyParamDoc
 * @param  func             MyParamDoc
 */
void traverse(List * plist, void (*func)(Item item, int index)){
    Node * current = *plist;
    int count = 0;
    while(current != NULL){
        (*func)(current->item, count + 1);
        current = current->next;
        count++;
    }
}


unsigned int itemCount(List * plist){
    unsigned int count = 0;
    Node * current = *plist;
    while(current != NULL){
        count++;
        current = current->next;
    }
    return count;
}

Item * getItem(List * plist, int index){
    Node * current = *plist;
    int count = 0;
    while((current != NULL)){
        if(count == index) {
            return &current->item;
        }
        current = current->next;
        count++;
    }
    return NULL;
}

void emptyList(List * plist){
    Node * temp;
    while((*plist) != NULL){
        temp = *plist;
        free(*plist);
        *plist = temp->next;
    }
}

static void copyItemToNode(Item item, Node * pnode){
    pnode->item = item;
}


